package com.example.demo.Controller;

import com.example.demo.Services.impl.TjServicesImpl;
import com.example.demo.Services.impl.UserServicesImpl;
import com.example.demo.entiy.*;
import com.example.demo.utils.MyUtils;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import sun.security.provider.MD5;
import sun.security.rsa.RSASignature;

import javax.naming.NamingEnumeration;
import javax.servlet.http.HttpSession;
import java.util.LinkedList;
import java.util.List;

@Controller
@RequestMapping("/tj")
public class index {
    @Autowired
    TjServicesImpl tjServices;
    @Autowired
    UserServicesImpl userServices;
    public static HttpSession session;
    MyUtils myUtils= new MyUtils();
    @RequestMapping(value = "/",method = RequestMethod.GET)
    public String index(){
            return "/tj/index";
    }
    @RequestMapping(value = "/test",method = RequestMethod.POST)
    @ResponseBody
    public Object login(String username,String password,int sess,HttpSession sessions){
        String status="true";
        String msg="";
        Object data="";
        session=sessions;
        String uri="";
        if (sess==1){
            uri="/dw/?sess="+1;
        }else {
            uri="/dw/?sess="+2;
        }
        session.setAttribute("sess",sess);
        try {
            String chars=username.substring(12,18).toLowerCase();//转成小写
            if (Integer.parseInt(username.substring(0,1))!=9){
                data=tjServices.selectEmpidx(username,password,1);
                if (tjServices.selectPsw(username,password,1).equals( password)){
                    if (data.equals("")){
                        msg="对不起，体检结果尚未公布";
                        status="false";
                    }else {
                        msg="/tj/bgd?empidx="+data+"&sess="+sess;
                    }
                    session.setAttribute("username",username+"-login");
                    session.setAttribute("password", password);
                    session.setMaxInactiveInterval(10000);
                }else if (chars.equals(password)){
                    if (data.equals("")){
                        msg="对不起,体检结果尚未公布";
                        status="false";
                    }else if (data.equals(0)){
                        msg="对不起,您未在本院体检过";
                        status="false";
                    }
                    else {
                        session.setAttribute("idx",data);
                        msg="/tj/bgd?empidx="+data+"&sess="+sess;
                    }
                    session.setAttribute("username",username+"-login");
                    session.setAttribute("password", password);
                    session.setAttribute("type",1);
                    session.setMaxInactiveInterval(10000);
                }else {
                    msg="密码有误";
                    status="false";
                }
            }
        }catch (Exception e){
            String psd=userServices.selectPWd(username);
            if (psd.equals(password)){
                msg=uri;
                session.setAttribute("username",username+"-login");
                session.setAttribute("password", password);
                session.setAttribute("type",2);
                session.setMaxInactiveInterval(10000);
            }else if (tjServices.selectPsw(username,password,2).equals(password)){
                data=tjServices.selectEmpidx(username,password,2);
                msg="/tj/bgd?empidx="+data+"&sess="+sess;
                session.setAttribute("username",username+"-login");
                session.setAttribute("password", password);
                session.setAttribute("type",1);
                session.setMaxInactiveInterval(10000);

            }else {
                status="false";
                msg="对不起，帐户名密码有误";
            }
        }
        return ReturnModel.getModel(msg,status,data);
    }
    @RequestMapping(value = "/bgd",method = RequestMethod.GET)
    public String tjdcx(int empidx,int sess, Model model){
        try {
            empidx=empidx==Integer.parseInt(session.getAttribute("idx").toString()) ? empidx:0;
        }catch (NullPointerException e){
            return "/tj/index";
        }

        if (empidx!=0) {
            List<Bgd> li = tjServices.selectAll(empidx);
            session.setAttribute("idx2",myUtils.returnIdx2(li));
            model.addAttribute("li",li);
        }else {
            return "/tj/index";
        }
        if (sess==1) {
            return  myUtils.renturnUrl("/tj/tjd",model);
        }else {
            return  myUtils.renturnUrl("/tj/tjd-sj",model);
        }
    }
    @RequestMapping("/my")
    public String tjdjt(String idx,int sess,Model model){
        List<String> ls;
        try{
            ls=(List<String>) session.getAttribute("idx2");
        }catch (NullPointerException e){
            return "/tj/index";
        }
        if (ls.contains(idx)) {
            if (myUtils.isTrueUrl(session)) {
                Bgd bgd = new Bgd();
                bgd = tjServices.selectOne(Integer.parseInt(idx));
                model.addAttribute("bgd", bgd);
                List<EmploymentHistory> li = tjServices.selEHByIdx(idx);
                List<PastMedicalHistory> l = tjServices.selPMHByIDX(idx);
                List<BirthHistory> l1 = tjServices.selBhByIDX(idx);
                List<MenstrualHistory> l2 = tjServices.selMHByIdx(idx);
                List<zonghe> l3 = tjServices.selZhByIDX(idx);
                model.addAttribute("zh", l3);
                model.addAttribute("mh", l2);
                model.addAttribute("bh", l1);
                model.addAttribute("eh", li);
                model.addAttribute("pmh", l);
                List<XM> list = myUtils.returnTureList(tjServices.selectXm(Integer.parseInt(idx)));
                List<XM> li1 = new LinkedList<>();
                List<XM> li2 = new LinkedList<>();
                for (int i = 0; i < list.size(); i++) {
                    if (i <= (list.size() / 2)) {
                        li1.add(list.get(i));
                    } else {
                        li2.add(list.get(i));
                    }
                }
                System.out.println(li2);
                model.addAttribute("li1", li1);
                model.addAttribute("li2", li2);
                if (sess == 1) {
                    return "/tj/tjdjt";
                } else {
                    return myUtils.renturnUrl("/tj/tjdjt-sj", model);
                }
            } else {
                return "/tj/index";
            }
        }else {
            return "/tj/index";
        }

    }


}
